(function($){

//点击购物车中的数量按钮，对应的数量发生变化
$(window).load(function(){
	//判断用户是否登录，如果登录成功，则获取cookie中的用户名并显示到登录和注册的地方；
	if($.cookie('isLogin')=='true'){
		$('.header-user').html('<a href="javascript:;" class="login">欢迎您，15201061391</a><a href="javascript:;" class="exit">[退出]</a><span class="line">|</span><a href="javascript:;" class="register">我的订单</a><span class="line">|</span><a href="javascript:;" class="register">购物车</a><span class="line">|</span><a href="javascript:;" class="register">我的收藏</a><span class="line">|</span><span class="ico-phone"></span>400-080-1888<span class="line">|</span><a href="javascript:;" class="da">下载达令<div class="download"><span class="arrow-7-5"></span><span class="pic-code"></span></div></a>')
	//点击退出按钮，清空cookie中的登录判断isLogin
		$('.exit').click(function(){
			$.cookie('isLogin',null,{path:'/'});
			window.location.href='shopping-car.html';
		})
	}
	//判断是否有物品加入购物车，若有，则改变html结构
	//先解析cookie，因为是对象的序列化呈现，所以先转化为对象，在进行操作
	var ifn = $.cookie('inf');
	var oCookie = JSON.parse(ifn);
	if(oCookie.cartBtag == true){
		//先将空购物车移除HTML
		$('.data-empty').css('display','none');
		$('.total-bo').css('display','block');
		$('.cart-list').css('display','block');
		$shop_ul = $('.goods.clear').clone();
		//通过克隆的方式来进行列表的添加
		//将创建好的列表放入cart-table中
		$('.cart-list').append($shop_ul);
		$('.goods.clear').eq(1).css('display','block');
	//初始化购车车中商品数量
		var shopNumber = 1;
	//解析cookie中被字符串序列化的对象
		var cart_inf_str = $.cookie('inf');
		var cart_inf = JSON.parse(cart_inf_str);
		var title = cart_inf.title;//获取标题
		var pic = cart_inf.pic;//获取图片路径
		var price = cart_inf.price;//获取商品单价
		shopNumber = parseInt(cart_inf.number);
		var cartBtag = cart_inf.cartBtag;
		console.log(cart_inf);
		console.log(shopNumber);
	//开始设置购物车中的商品信息
		$('.pic_src').css('background-image','url(../images/LTV-Small1.jpg)');
		$('.title').text(title);
		$('.price').text(price);
		$('.number-in').val(shopNumber);
		$('span.total').text($('.number-in').val()*price);
		$('span.f20').text($('.number-in').val()*price);//总金额
	}
	$('.ico-add').parent().click(function(){
		shopNumber++;
		console.log(shopNumber)
		$('.number-in').val(shopNumber);
		$('span.total').text($('.number-in').val()*price);
		$('span.f20').text($('.number-in').val()*price);//总金额
	});
	$('.ico-reduce').parent().click(function(){
		if(shopNumber<=1){
			shopNumber=1;
		}else{
			shopNumber--;
			$('.number-in').val(shopNumber);
			$('span.total').text($('.number-in').val()*price);
			$('span.f20').text($('.number-in').val()*price);//总金额
		}
	//点击删除按钮，购物车清空，此时将cookie中的商品信息的标志位变为false;
	});
	$('.delete11').click(function(){
		var cart_inf_str = $.cookie('inf');
		var cart_inf = JSON.parse(cart_inf_str);
		var title = cart_inf.title;//获取标题
		var pic = cart_inf.pic;//获取图片路径
		var price = cart_inf.price;//获取商品单价
		var cartBtag = cart_inf.cartBtag;
		var inf = { 'title' : title,'src' : pic, 'price' : price, 'cartBtag' : false };
		var inf_str = JSON.stringify(inf);
		$.cookie('inf',inf_str,{ expires : 7, path : "/" });
	})
})
	
})(jQuery)